package com.frain.springcloud.menu;

import com.alibaba.fastjson.JSONObject;
import com.frain.springcloud.common.APPConstant;
import com.frain.springcloud.common.AccessTokenUtil;
import com.frain.springcloud.menu.entity.SubButton;
import com.frain.springcloud.menu.entity.ViewTypeButton;
import com.frain.springcloud.util.HttpRequest;

public class CreateMenu {

    private static final String REGISTER_KEY = "REGISTER";

    private static final String LOGIN_KEY = "LOGINiNFO";

    private static final String url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=";

    static String loginPath = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + APPConstant.APP_ID
            + "&redirect_uri=http://www.supplychainfinance.top/user/accessUser&response_type=code&scope=snsapi_userinfo&state=123456#wechat_redirect ";

    static String findPath = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + APPConstant.APP_ID
            + "&redirect_uri=http://www.supplychainfinance.top/user/findUser&response_type=code&scope=snsapi_userinfo&state=123456#wechat_redirect ";

    public static void main(String[] args) throws Exception {
        JsonMenu menu = createJsonMenu();

        String path = url + AccessTokenUtil.getToken();
        String jsonString = JSONObject.toJSONString(menu);
        String sendPost = HttpRequest.sendPost(path, jsonString, null);

        System.out.println(jsonString);
        System.out.println(sendPost);

    }

    private static JsonMenu createJsonMenu() {

        JsonMenu jsonMenu = new JsonMenu();

        ViewTypeButton baidu = new ViewTypeButton();
        baidu.setName("百度一下");
        baidu.setUrl("http://www.baidu.com");

        SubButton caidan = new SubButton();
        caidan.setName("菜单");

        ViewTypeButton click = new ViewTypeButton();
        click.setName("注册");
        click.setUrl(loginPath);
        caidan.getSub_button().add(click);

        ViewTypeButton login = new ViewTypeButton();
        login.setName("登陆");
        login.setUrl(findPath);
        caidan.getSub_button().add(login);

        jsonMenu.getButton().add(baidu);
        jsonMenu.getButton().add(caidan);

        return jsonMenu;

    }

}
